package com.mew.slots.core.game.dwsl.room;

import cn.hutool.core.util.RandomUtil;
import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.game.dwsl.bo.DwslInRoom;
import com.mew.slots.core.game.dwsl.bo.DwslPlayMsgs;
import com.mew.slots.core.game.dwsl.roller.Controlline;

import java.util.ArrayList;

/**
 * @author dylin
 */
public class DwslRoom extends BaseRoom {

    public static int _FREE = 11;
    public static int _SCATTER = 12;

    private int freeRate;
    private DwslInRoom inRoom;
    public DwslRoom(int freeRate,DwslInRoom inRoom){
        this.freeRate = freeRate;
        this.inRoom = inRoom;
        res = new DwslPlayMsgs();
        row = 3;
        col = 5;
        _WILD = 10;
        spes = new ArrayList<Integer>(){{
            add(11);
            add(12);
        }};

         rate = new int[][]{
                 {0, 0,  5, 15, 50},      //方块           0
                 {0, 0,  5, 15, 50},      //梅花           1
                 {0, 0,  5, 15, 50},      //红桃           2
                 {0, 0,  5, 15, 50},      //黑桃           3

                 {0, 0, 10, 25, 100},      //红毛猫          4
                 {0, 0, 10, 25, 100},      //红毛猫          5
                 {0, 0, 10, 25, 100},      //红毛猫          6

                 {0, 0, 10, 30, 150},      //红毛猫          7
                 {0, 0, 10, 30, 150},      //红毛猫          8
                 {0, 0, 10, 30, 150},      //红毛猫          9

                 {0, 0, 20,  50,200},      //WILD           10
                 {0, 0,  0,   0,  0},      //FREE           11
                 {0, 0,  0,   0,  0},      //SCATTER        12
        };

         alllines = new int[][]{
                 {5,6,7,8,9},
                 {0,1,2,3,4},
                 {10,11,12,13,14},
                 {0,6,12,8,4},
                 {10,6,2,8,14},
                 {5,1,7,13,9},
                 {5,11,7,3,9},
                 {0,1,7,13,14},
                 {10,11,7,3,4},
                 {0,6,2,8,4},
                 {10,6,12,8,14},
                 {5,1,2,3,9},
                 {5,11,12,13,9},
                 {0,6,7,8,4},
                 {10,6,7,8,14},
                 {5,6,2,8,9},
                 {5,6,12,8,9},
                 {5,1,7,3,9},
                 {5,11,7,13,9},
                 {0,1,2,8,14},
                 {10,11,12,8,4},
                 {0,6,12,13,14},
                 {10,6,2,3,4},
                 {5,6,7,3,14},
                 {5,6,7,13,4},
                 {0,6,7,8,14},
                 {10,6,7,8,4},
                 {10,11,7,3,9},
                 {0,1,7,13,9},
                 {10,6,12,13,14},
                 {0,6,2,3,4},
                 {0,6,12,8,9},
                 {10,6,2,8,9},
                 {5,1,7,3,4},
                 {5,11,7,13,14},
                 {0,6,7,8,9},
                 {10,6,7,8,9},
                 {5,1,2,3,4},
                 {5,11,12,13,14},
                 {5,6,7,8,4},
                 {5,6,7,8,14},
                 {0,1,7,3,4},
                 {10,11,7,13,14},
                 {0,1,2,8,4},
                 {10,11,12,8,14},
                 {5,6,2,3,4},
                 {5,6,12,13,14},
                 {0,1,7,8,4},
                 {10,11,7,8,14},
                 {10,6,2,8,4}
         };

    }
    @Override
    public void getLinesXml(){

        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getLineInts(free?1:0,rollerID);
        }
        res.endSlots=endSlots.clone();
        if(free) {
            ((DwslPlayMsgs) res).setWns(inRoom.getWns());
        }else{
            computeWildBonus();
        }
    }

    private void computeWildBonus(){
        c:for (int c = 0; c < col; c++) {
            r:for (int r = 0; r < row; r++) {
                if(endSlots[r*col+c] == _WILD){
                    inRoom.getWns()[c]++;
                    continue c;
                }
            }
        }
        DwslPlayMsgs pmres=(DwslPlayMsgs) res;
        pmres.setWns(inRoom.getWns().clone());
        for (int i = 0; i < inRoom.getWns().length; i++) {
            if(inRoom.getWns()[i] >= 3){
                endSlots[i] = _WILD;
                endSlots[i+col] = _WILD;
                endSlots[i+col*2] = _WILD;
                inRoom.getWns()[i] = 0;
            }
        }
    }

    @Override
    public void freeBouns() {
        computeFree();
        computeBonus();
    }
    private void computeFree(){
        DwslPlayMsgs pmres=(DwslPlayMsgs) res;
        int freeNum = fsdb(_FREE);
        if(freeNum > 2){
            pmres.setFree((byte)1);
            pmres.setFreeNumber(5);
            if(!free){
                pmres.setFreeRate(RandomUtil.randomInt(2,5));
            }
        }
        if(free){
            pmres.setMultiple(res.getMultiple() * freeRate);
        }
    }
    private void computeBonus(){
        int sNum = fsdb(_SCATTER);
        if(sNum > 2){
            res.setBouns(1);
        }
    }

    @Override
    public int[] control(){
        Controlline controlline=new Controlline();
        switch (controlType) {
            case 1:
                return bigwin();
            case 2:
                return controlline.free();
            case 3:
                return controlline.bonus();
            default:
                return controlline.empty();
        }
    }
}
